package com.sunfeng.n4.crossoutput;

import lombok.extern.slf4j.Slf4j;

/**
 * 固定顺序运行程勋WaitNotify版本
 *
 * @author sunfeng
 * @date 2021/03/30 14:32
 **/
@Slf4j(topic = "c.WaitNotify")
public class WaitNotify {
    private static final Object lock = new Object();

    private static boolean condition = false;

    public static void main(String[] args) {
        new Thread(() -> {
            synchronized (lock) {
                while (!condition) {
                    try {
                        lock.wait();

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                log.debug("输出数字:{}", 1);
            }

        }).start();

        new Thread(() -> {
            synchronized (lock) {

                log.debug("输出数字:{}", 2);
                condition = true;
                lock.notifyAll();

            }

        }).start();

    }
}
